package com.ssm.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.ssm.subject.application.convert.SubjectLikedDTOConvert;
import com.ssm.subject.application.dto.SubjectInfoDTO;
import com.ssm.subject.application.dto.SubjectLikedDTO;
import com.ssm.subject.common.entity.Result;
import com.ssm.subject.common.util.LoginUtil;
import com.ssm.subject.domain.bo.SubjectLikedBO;
import com.ssm.subject.domain.service.SubjectLikedDomainService;

import com.ssm.subject.common.entity.PageResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subjectLiked")
@Log4j2
public class SubjectLikedController {
    @Resource
    private SubjectLikedDomainService subjectLikedDomainService;

    /**
     * 新增/取消点赞
     * @param subjectLikedDto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectLikedDTO subjectLikedDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.add.dto:{}", JSON.toJSONString(subjectLikedDto));
            }
            Preconditions.checkNotNull(subjectLikedDto.getSubjectId(), "题目id不能为空");
            Preconditions.checkNotNull(subjectLikedDto.getStatus(), "点赞状态不能为空");
            subjectLikedDto.setLikeUserId(LoginUtil.getLoginId());
            Preconditions.checkNotNull(subjectLikedDto.getLikeUserId(), "点赞人不能为空");
            SubjectLikedBO subjectLikedBO = SubjectLikedDTOConvert.INSTANCE.subjectLikedDtoToBo(subjectLikedDto);
            subjectLikedDomainService.add(subjectLikedBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.info("SubjectLikedController.add.error:{}", e.getMessage(), e);
            return Result.fail("题目点赞失败");
        }
    }

    /**
     * 分页查询点赞记录
     * @return
     */
    @PostMapping("/getSubjectLikedPage")
    public Result<PageResult<SubjectLikedDTO>> getSubjectLikedPage(@RequestBody SubjectLikedDTO subjectLikedDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectLikedController.getSubjectLikedPage.dto:{}", JSON.toJSONString(subjectLikedDTO));
            }
            SubjectLikedBO subjectLikedBO = SubjectLikedDTOConvert.INSTANCE.subjectLikedDtoToBo(subjectLikedDTO);
            PageResult<SubjectLikedBO> subjectLikedBOList = subjectLikedDomainService.getSubjectLikedPage(subjectLikedBO);
            //直接返回BO，转DTO繁琐
            return Result.ok(subjectLikedBOList);
        } catch (Exception e) {
            log.info("SubjectLikedController.getSubjectLikedPage.error:{}", e.getMessage(), e);
            return Result.fail("查询点赞记录失败");
        }
    }
}